package uk.ac.salfordcc.sccmobile;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class StoreCredentials extends Activity {

	
	EditText username;
	EditText password;
	Button save;




	
	
	
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_credentials);
		
		

		
		username = (EditText)findViewById(R.id.et_username);
		password = (EditText)findViewById(R.id.password);
		save = (Button)findViewById(R.id.saveCredentials);
		//save.setOnClickListener((android.view.View.OnClickListener) this);
		loadPreferences();
		
		//dd
		

		
		
	}
	
	
	
	private void loadPreferences(){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		String storedUsername = sp.getString("USERNAME", ""); //returns "" if not stored.
		username.setText(storedUsername);
		
		Log.e("USERNAME:", username.toString());
	}
	
	
	private void savePreferences(String key, String value){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		Editor edit = sp.edit();
		edit.putString(key, value);
		edit.commit();
		Log.e("MESSAGE","SAVED");
		
		loadPreferences();
	}


	public void saveSettings(View view) {
		// TODO Auto-generated method stub
		savePreferences("USERNAME", username.getText().toString());
		savePreferences("PASSWORD", password.getText().toString());
		
		finish();
	}	
	
}
